﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Student
{
    public partial class InsertInfo : Form
    {
        public InsertInfo()
        {
            InitializeComponent();
        }

        //程序加载时开启皮肤
        private void InsertInfo_Load(object sender, EventArgs e)
        {
            //加载皮肤
            this.skinEngine1.Active = true;
            this.skinEngine1.SkinFile = "skin/Page.ssk";
        }

        //提交
        private void btn_Add_Click(object sender, EventArgs e)
        {
            //获取文章标题
            string title = this.txt_Title.Text.Trim();
            //获取文章作者
            string author = this.txt_Author.Text.Trim();
            //获取是否推荐
            string recommend = radioButton1.Checked ? "是" : "否";
            //获取创建时间
            string createdTime = this.txt_CreatedTime.Text.Trim();
            
            //判断用户输入的内容是否为null
            var isTitle = string.IsNullOrEmpty(title);
            var isAuthor = string.IsNullOrEmpty(author);

            //调用DbHelper类中GetIDU方法
            if (!isTitle && !isAuthor)
            {
                string sql = string.Format("insert into Articles(Title,Author,Recommend,CreatedTime) values('{0}','{1}','{2}','{3}')", title, author, recommend, createdTime);
                var result = DbHelper.GetIDU(sql);
                //显示信息
                if (result >= 1)
                {
                    MessageBox.Show("增加成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    StuInfoManagementSystem stu = new StuInfoManagementSystem();
                    stu.Show();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("增加失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else if (isTitle && isAuthor)
            {
                MessageBox.Show("文章标题和作者不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (isTitle)
            {
                MessageBox.Show("文章标题不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (isAuthor)
            {
                MessageBox.Show("文章作者不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("增加失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        //取消
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            //关闭窗口
            this.Close();
            StuInfoManagementSystem stu = new StuInfoManagementSystem();
            stu.Show();
        }
    }
    
 }

